package com.sense.kukoo.biz_library.adapter.provider

import com.sense.kukoo.biz_library.adapter.SubscriptionsAdapter
import com.sense.kukoo.common.R
import com.sense.kukoo.common.repo.bean.BookItemBean
import com.sense.kukoo.common.repo.bean.LibrarySubscriptionBean
import com.sense.kukoo.common.repo.bean.SubscriptionsBean
import com.sense.kukoo.common.wiget.SubscriptionsChannelView
import com.sense.kukoo.common.wiget.adapter.provider.MultiItemProvider
import com.sense.kukoo.common.wiget.adapter.viewholder.BaseViewHolder

class ChannelItemProvider(val listener: SubscriptionsAdapter.SubscriptionsEventListener):
    MultiItemProvider<SubscriptionsBean>() {

    private val TAG: String = javaClass.simpleName

    override fun getItemType(): Int = SubscriptionsBean.TYPE_CHANNEL

    override fun getItemLayoutId(): Int = R.layout.common_subscriptions_channel_container

    override fun onBindViewHolder(viewHolder: BaseViewHolder, item: SubscriptionsBean) {
        val view = viewHolder.getView(R.id.item) as SubscriptionsChannelView
        val channelItem:List<LibrarySubscriptionBean> = item.channelItem!!
        var len = channelItem.size
        len = if(len > 8) 8 else len
        view.setSize(len)
        view.items.forEachIndexed { index, itemView ->
            val bean:LibrarySubscriptionBean = channelItem[index]
            //
            itemView.setRedDot(bean.isShowRedDot())
            //
            itemView.setIconUrl(bean.book.coverUrl)
        }
        //
        view.setListener(object :SubscriptionsChannelView.OnChannelItemEventListener{
            override fun onSeeAll() {
                listener.onChannelSeeAll(item)
            }

            override fun onChannelItemClick(position: Int) {
                listener.onChannelItemClick(item, position)
            }
        })
    }
}